package com.bw.module_patrol.view

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_patrol.R
import com.bw.module_patrol.adapter.PatrolAdapter
import com.bw.module_patrol.databinding.ActivityPatrolBinding
import com.bw.module_patrol.viewmodel.PatrolViewModel

/**
 * 工单-智慧园区-巡逻管理
 */
@Route(path = RouterPath.PATH_PATROL_ACTIVITY)
class PatrolActivity : BaseMvvmActivity<ActivityPatrolBinding>() {

    private val vm by lazy { ViewModelProvider(this)[PatrolViewModel::class.java] }
    private val patrolAdapter = PatrolAdapter()
    override fun observeDataChange() {
        vm.patrolSuccess.observe(this){
            patrolAdapter.data.clear()
            patrolAdapter.data.addAll(it)
            patrolAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        mDataBinding.imageView.setOnClickListener { finish() }

        mDataBinding.rv.adapter = patrolAdapter
        mDataBinding.rv.layoutManager = LinearLayoutManager(this)
        mDataBinding.imageView.setOnClickListener {
            finish()
        }
        patrolAdapter.addChildClickViewIds(R.id.edit)
        patrolAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.edit->{
                    ARouter.getInstance().build(RouterPath.PATH_EDIT_PATROL_ACTIVITY).withInt("patrolId",patrolAdapter.data[position].id).navigation()
                }
            }
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_patrol

    override fun onResume() {
        super.onResume()
        vm.getPatrol()
    }
}